package cn.swu.lzr.event;

import cn.swu.lzr.JDBC.DBTools;
import cn.swu.lzr.page.Page;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.Writer;

public class UpdateHtml extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        int id = Integer.valueOf(request.getParameter("eventId"));
        HttpSession session = request.getSession();
        session.setAttribute("Updateid",id);
        try {
            DBTools dbTools = new DBTools();
            Event event = dbTools.GetEventById(id);
            String template = """
                 <form action="insertevent" method="post" enctype="multipart/form-data">
                        <input type="hidden" name="id" value="%s"/>
                        日期：<input type="text"  name="date" value="%s"><br><br>
                        记录：<input type="text" name="event" value="%s"><br><br>
                        图片：<input type="file" name="picture"/> <br><br>
                        <input type="submit" value="提 交">
                </form>
            """;
            String form = String.format(template,event.getId(),event.getDate(),event.getEvent());

            String html = Page.wrap(form);

            response.setContentType("text/html");
            try(Writer writer = response.getWriter()) {
                writer.write(html);
            }
        } catch (Exception e) {
        e.printStackTrace();
        }

    }
}
